package com.example.lenovo.downloaddemo.activity;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.lenovo.downloaddemo.R;
import com.example.lenovo.downloaddemo.bean.Customer;
import com.example.lenovo.downloaddemo.bean.LoginBean;
import com.example.lenovo.downloaddemo.bean.ReturnBean;
import com.example.lenovo.downloaddemo.http.datasource.MDataSource;
import com.example.lenovo.downloaddemo.http.okhttp.utils.AppContext;
import com.example.lenovo.downloaddemo.http.okhttp.utils.Collintion;
import com.example.lenovo.downloaddemo.http.okhttp.utils.CommonUtils;
import com.example.lenovo.downloaddemo.http.okhttp.utils.MD5;
import com.example.lenovo.downloaddemo.http.okhttp.utils.SharedPreUtils;
import com.example.lenovo.downloaddemo.http.okhttp.utils.ToastUtil;
import com.example.lenovo.downloaddemo.sqlite.LoadListCallBack;
import com.example.lenovo.downloaddemo.util.GotoActivity;
import com.example.lenovo.downloaddemo.util.StatusBarUtil;
import com.example.lenovo.downloaddemo.util.TimeUtil;
import com.example.lenovo.downloaddemo.view.TitleBarView;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Request;

public class LoginActivity extends BaseActivity implements View.OnClickListener {
    @Bind(R.id.login_title)
    TitleBarView login_title;
    @Bind(R.id.register_link)
    TextView register_link;//快速注册
    @Bind(R.id.login_username)//用户名
    EditText login_username;
    @Bind(R.id.login_password)
    EditText login_password;//密码
    @Bind(R.id.login_comfirm_button)
    TextView login_comfirm_button;//登陆
    @Bind(R.id.login_page_find_password)
    TextView login_page_find_password;//找回密码
    private String allString;
    private Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==1){
                String returnMessage= (String) msg.obj;
                Gson gson=new Gson();
                try{
                    LoginBean bean=gson.fromJson(returnMessage,LoginBean.class);
                    if(bean.getCode()==200){

                        if (!CommonUtils.isEmpty(bean.getMsg().getToken())) {
                            //保存用户登录信息
                            SharedPreUtils.setStringToPre(LoginActivity.this,
                                    "phone", bean.getMsg().getUser_phone());
                            SharedPreUtils.setStringToPre(LoginActivity.this,
                                    "token", bean.getMsg().getToken());
                            SharedPreUtils.setStringToPre(LoginActivity.this,
                                    "nickname", bean.getMsg().getNickname());
                            Log.e("存储===","存储成功");
                            String phone= SharedPreUtils.getPreStringInfo(AppContext.getInstance(), "phone");
                            String name= SharedPreUtils.getPreStringInfo(AppContext.getInstance(), "nickname");
                            String token= SharedPreUtils.getPreStringInfo(AppContext.getInstance(), "token");
                            Log.e("====",phone+"====\n"+name+"===\n"+token);
//                        Customer customer = AppContext.getInstance().getCustomer();
//                        customer.setToken(bean.getMsg().getToken());
//                        customer.setCustomerName(bean.getMsg().getUser_phone());
                            GotoActivity.gotoActiviy(LoginActivity.this, MainActivity.class);
                        }

//                    GotoActivity.gotoActiviy(LoginActivity.this,MainActivity.class);
                        finish();
                    }
                }catch (Exception e){
                    ReturnBean bean1 =gson.fromJson(returnMessage,ReturnBean.class);
                    ToastUtil.makeText(AppContext.getInstance(), bean1.getMsg());
                }


            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        initView();
        initListener();
    }

    @Override
    protected void initView() {
        StatusBarUtil.setMargin(AppContext.getInstance(),login_title);
        login_title.setTitle(AppContext.getInstance().getResources().getString(R.string.login));
        login_title.showBackButton(false);

        register_link.setText(AppContext.getInstance().getResources().getText(R.string.quick_register));
        login_comfirm_button.setText(AppContext.getInstance().getResources().getText(R.string.login));
        login_page_find_password.setText(AppContext.getInstance().getResources().getText(R.string.find_password));

        login_username.setHint(AppContext.getInstance().getResources().getText(R.string.tel_ch));
        login_password.setHint(AppContext.getInstance().getResources().getText(R.string.regist_inputPhone_cn));

    }



    @Override
    protected void initListener() {
        register_link.setOnClickListener(this);
        login_comfirm_button.setOnClickListener(this);
        login_page_find_password.setOnClickListener(this);
    }
    private void login_Confrim(String phone,String password){
        HashMap<String, String> map = new HashMap<>();
        map.put("model", "Login");
        map.put("method", "login");
        map.put("user_phone", phone);
        map.put("password", password);
        map.put("time",new TimeUtil().getMoment());
        Collintion collintion=new Collintion();

        allString=collintion.Sort(map)+"ad1af567dc7bfedd5e760fc1f48a59ae";
        String sign= MD5.GetMD5Code(MD5.GetMD5Code(allString));
        Log.e("拼接字符串",allString);
        Log.e("签名===",sign);
        map.put("sign",sign);
        MDataSource<Customer> dataSource = new MDataSource<>(AppContext.getInstance());
        dataSource.postData(map, new LoadListCallBack() {
            @Override
            public void loadList(List list) {

            }

            @Override
            public void systemError(Request request, String errorInfo, Exception e) {
                Log.e("error", "systemError: " + errorInfo);
                e.printStackTrace();
            }

            @Override
            public void retLoad(String code) {

            }

            @Override
            public void loadString(String string) {
                Log.e("登陆", "loadString 获取结果 : " + string);

                Message message=Message.obtain();
                message.what=1;
                message.obj=string;
                mHandler.sendMessage(message);





            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.register_link:
                Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
                break;
            case R.id.login_comfirm_button:
                getData();
                break;
            case R.id.login_page_find_password:
                GotoActivity.gotoActiviy(LoginActivity.this,FindPassWordActivity.class);
                break;
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        finish();
    }


    private void getData() {
       String phone= login_username.getText().toString().trim();
       String password=login_password.getText().toString().trim();
       if(!TextUtils.isEmpty(phone)&&!TextUtils.isEmpty(password)){
           if(CommonUtils.isNetOk(AppContext.getInstance())) {
               login_Confrim(phone, password);
           }else{
               ToastUtil.makeText(AppContext.getInstance(),"Fail!");
           }
       }else{
           ToastUtil.makeText(AppContext.getInstance(),"输入不能为空");
       }
    }
}
